I have a documents with a bunch of variables(in Object Text). This document is shared by several projects and each project has its own values for the variables. I also have a variable list which provides the values of the variables for each project.
I'm trying to use some DXL to replace all the variables with values for a specific project and save the result in another attribute (eg., projectName_Object Text) in the same document. The same formatting of Object Text needs to be kept in projectName_Object Text. The high level structure of the DXL looks like below:
Module m = current \\this is the document
Module vl = read("variable list",false,true)\\this is the variable list
string resTxt,varValue
Object o,vobj
int offset,len
for o in m do
{
resTxt = richText(o."Object Text")
for vobj in vl do
{
...
varValue = vobj."Variable Value"
while(findRichText (resTxt, var, offset, len, true))
{
resTxt = replaceRichText (resTxt, offset, len, varValue)
}
}
}
Apparently, this DXL would incurr Massive usage of strings, which tends to cause memory exhaust problems.
Can anyone suggest a way to optimize the code to solve the problem?
I'm thinking if there are functions similar to findRichText and replaceRichText but accept Buffer instead of string as input, I would be able to avoid the problem. However, I haven't managed to find such a function.
Any kind of help would be appreciated.
njiang - Fri Jul 20 15:15:19 EDT 2012 |
|
Re: How to avoid Memory Leaking while dealing RichText subString problems? njiang - Fri Jul 20 15:21:24 EDT 2012
Missed one line in the code, each time exits the inner loop,
o."projectName_Object Text" = richText(resTxt)
|
|
Re: How to avoid Memory Leaking while dealing RichText subString problems? llandale - Mon Jul 23 11:38:34 EDT 2012
Your findRichText->replaceRichText combination works?? That did not work years ago; looks like I need to rethink things. Anyway:
-
You need resTxt = richTextWithOle(o."Object Text")
-
I think you are missing: var = vobj."Variable Name"
-
This code is a prime candidate for Attr-DXL, as at it's core it derives info about an object based on other info of that object. I think you will find this also resolves your memory problem.
-Louie
|
|
Re: How to avoid Memory Leaking while dealing RichText subString problems? njiang - Mon Jul 23 12:15:03 EDT 2012 llandale - Mon Jul 23 11:38:34 EDT 2012
Your findRichText->replaceRichText combination works?? That did not work years ago; looks like I need to rethink things. Anyway:
-
You need resTxt = richTextWithOle(o."Object Text")
-
I think you are missing: var = vobj."Variable Name"
-
This code is a prime candidate for Attr-DXL, as at it's core it derives info about an object based on other info of that object. I think you will find this also resolves your memory problem.
-Louie
Thank you for reply.
I did notice a few omissions when using findRichText->replaceRichText combination, but most of the replacements were done correctly. What other functions provide similar functionality? I have to keep the formatting of the results.
The code I posted here is not the real code so sth. got missed by mistake.
I can't make the script to be attr-dxl, because the real code also provides an user interface where user can select project from a drop down list and also browse the variable list they are going to use.
I believe the memory problem is caused by too many large strings:
.Rich text format makes short strings long
.A string is considerred a new one if one variable in it is replaced. For example, "This problem shall be solved by using X and Y." To replace variables X and Y in the string, three different strings (the original, one with X replace, one with X and Y replaced) are created during the process.
If there are functions which would handle rich text buffers, I would be able to chop the strings and with the example above, the created strings would be "This problem shall be solved by using X and Y." and value of X and value of Y.
Hope I have explained my thoughts clear.
|
|
Re: How to avoid Memory Leaking while dealing RichText subString problems? Mathias Mamsch - Wed Aug 01 18:07:41 EDT 2012 njiang - Mon Jul 23 12:15:03 EDT 2012
Thank you for reply.
I did notice a few omissions when using findRichText->replaceRichText combination, but most of the replacements were done correctly. What other functions provide similar functionality? I have to keep the formatting of the results.
The code I posted here is not the real code so sth. got missed by mistake.
I can't make the script to be attr-dxl, because the real code also provides an user interface where user can select project from a drop down list and also browse the variable list they are going to use.
I believe the memory problem is caused by too many large strings:
.Rich text format makes short strings long
.A string is considerred a new one if one variable in it is replaced. For example, "This problem shall be solved by using X and Y." To replace variables X and Y in the string, three different strings (the original, one with X replace, one with X and Y replaced) are created during the process.
If there are functions which would handle rich text buffers, I would be able to chop the strings and with the example above, the created strings would be "This problem shall be solved by using X and Y." and value of X and value of Y.
Hope I have explained my thoughts clear.
Why are you not using buffers instead of strings? There is a buffer function:
Buffer replaceRichText (Buffer&, string, int, int, string)
and you can use 'tempStringOf' to pass the attribute value to findRichText. Maybe that helps, regards, Mathias
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
|
Re: How to avoid Memory Leaking while dealing RichText subString problems? njiang - Thu Aug 02 11:04:50 EDT 2012 Mathias Mamsch - Wed Aug 01 18:07:41 EDT 2012
Why are you not using buffers instead of strings? There is a buffer function:
Buffer replaceRichText (Buffer&, string, int, int, string)
and you can use 'tempStringOf' to pass the attribute value to findRichText. Maybe that helps, regards, Mathias
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Mathias, can you give more information on this buffer function? I can't find it in the DXL manual in DOORS. I'm using DOORS 9.3.0.6.
The function you gave doesn't seem to be able to save memory based on how it looks, because it still requires string as input.
Buffer replaceRichText (Buffer&, string, int, int, string)
Please advise. Thanks.
|
|
Re: How to avoid Memory Leaking while dealing RichText subString problems? Mathias Mamsch - Sat Aug 04 17:06:40 EDT 2012 njiang - Thu Aug 02 11:04:50 EDT 2012
Mathias, can you give more information on this buffer function? I can't find it in the DXL manual in DOORS. I'm using DOORS 9.3.0.6.
The function you gave doesn't seem to be able to save memory based on how it looks, because it still requires string as input.
Buffer replaceRichText (Buffer&, string, int, int, string)
Please advise. Thanks.
Hi,
so first of all - I found this function in the DXL perms list, which you can grab from here:
https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14580797�
A lot of DXL functions are undocumented, especially if it are variants of other existing functions with some undocumented/different parameters. From the
string replaceRichText(string, int, int, string)
I experimented around a bit and found that:
Buffer replaceRichText(Buffer &, string, int, int, string)
will do the same as replaceRichText, but return its value in the buffer passed as the first parameter, which must be allocated before the call.
Generally you have no problems with functions that take strings as parameters, since you can always use tempStringOf and pass that string parameter from a buffer without generating a string table entry. You are only screwed with functions that return a string, since you cannot avoid a string table entry in this case. Therefore the above function should suit you fine:
Buffer buf = create()
buf += "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1031{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}} \\viewkind4\\uc1\\pard\\f0\\fs20 "
buf += "AB\\b CD\\b0 YFG\\par
}
"
int iOfs = 0, iLen = 0
if (findRichText(tempStringOf buf, "CD", iOfs, iLen, false)) {
print "Found at: " iOfs ":" iLen "\n"
Buffer bufNew = create()
print "BufNew: " ((addr_ bufNew) int) "\n"
print "BufOld: " ((addr_ buf) int) "\n"
Buffer bufResult = replaceRichText (bufNew, tempStringOf buf, iOfs, iLen, "XY")
print "After BufNew: " ((addr_ bufNew) int) "\n"
print "After BufOld: " ((addr_ buf) int) "\n"
print "After BufResult: " ((addr_ bufResult) int) "\n"
print (tempStringOf bufResult) "\n"
}
However I find that this replaceRichText function is pretty stupid. In the above example it will NOT find the string "DY" since it has some richText formatting options in between, worse, if you search for "DE" it will also search for the string in the richText markup and replace the text there. I also got DOORS crashes.
Maybe that helps, regards, Mathias
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
|
Re: How to avoid Memory Leaking while dealing RichText subString problems? njiang - Thu Aug 16 14:11:11 EDT 2012 Mathias Mamsch - Sat Aug 04 17:06:40 EDT 2012
Hi,
so first of all - I found this function in the DXL perms list, which you can grab from here:
https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14580797�
A lot of DXL functions are undocumented, especially if it are variants of other existing functions with some undocumented/different parameters. From the
string replaceRichText(string, int, int, string)
I experimented around a bit and found that:
Buffer replaceRichText(Buffer &, string, int, int, string)
will do the same as replaceRichText, but return its value in the buffer passed as the first parameter, which must be allocated before the call.
Generally you have no problems with functions that take strings as parameters, since you can always use tempStringOf and pass that string parameter from a buffer without generating a string table entry. You are only screwed with functions that return a string, since you cannot avoid a string table entry in this case. Therefore the above function should suit you fine:
Buffer buf = create()
buf += "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1031{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}} \\viewkind4\\uc1\\pard\\f0\\fs20 "
buf += "AB\\b CD\\b0 YFG\\par
}
"
int iOfs = 0, iLen = 0
if (findRichText(tempStringOf buf, "CD", iOfs, iLen, false)) {
print "Found at: " iOfs ":" iLen "\n"
Buffer bufNew = create()
print "BufNew: " ((addr_ bufNew) int) "\n"
print "BufOld: " ((addr_ buf) int) "\n"
Buffer bufResult = replaceRichText (bufNew, tempStringOf buf, iOfs, iLen, "XY")
print "After BufNew: " ((addr_ bufNew) int) "\n"
print "After BufOld: " ((addr_ buf) int) "\n"
print "After BufResult: " ((addr_ bufResult) int) "\n"
print (tempStringOf bufResult) "\n"
}
However I find that this replaceRichText function is pretty stupid. In the above example it will NOT find the string "DY" since it has some richText formatting options in between, worse, if you search for "DE" it will also search for the string in the richText markup and replace the text there. I also got DOORS crashes.
Maybe that helps, regards, Mathias
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Thank you Mathias. The memory problem is solved after I followed your advise. However, the running time of the script now becomes extremely long. I guess that's the trade-off I have to make.
|
|
Re: How to avoid Memory Leaking while dealing RichText subString problems? Mathias Mamsch - Thu Aug 16 16:06:35 EDT 2012 njiang - Thu Aug 16 14:11:11 EDT 2012
Thank you Mathias. The memory problem is solved after I followed your advise. However, the running time of the script now becomes extremely long. I guess that's the trade-off I have to make.
You may use the allocated objects code from here:
https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14830975#14830975
to see if the slowdown maybe a result of you leaking any object. If not then probably the string table is the issue.
Regards, Mathias
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
|